/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOs;

import entidad.Actor;
import entidad.Artista;
import entidad.Director;
import entidad.Person;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Stiv
 */
public class DaoPerson {
    
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("GRLUPU");
    public void crear(Director object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
    
    public void crear(Actor object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
    
    public void crear(Artista object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
    
     public List<Person> AllPerson(){
        EntityManager em = emf.createEntityManager();
        List<Person> cat = new ArrayList<Person>();
        Query q= em.createQuery("SELECT u FROM Person u");
        try {
            List<Person> tempCat = q.getResultList();
        for(Person c: tempCat){
            Person prod = new Person();
            prod.setNombre(c.getNombre());
            prod.setApellido(c.getApellido());
            prod.setFecha(c.getFecha());
            prod.setImagen(c.getImagen());
            prod.setLugarNacimiento(c.getLugarNacimiento());

            cat.add(prod);
        }
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return cat;
        }
    }
     
     public Actor leerNombre(Actor par){
        EntityManager em = emf.createEntityManager();
        Actor prov=null;
        Query q= em.createQuery("SELECT u from Actor u "+
                "WHERE u.nombre = :name" )
                .setParameter("name", par.getNombre());
        try {
            prov = (Actor) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            prov = (Actor) q.getResultList().get(0);
            
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return prov;
        }
    }
     

     
     public Director leerNombre(Director par){
        EntityManager em = emf.createEntityManager();
        Director prov=null;
        Query q= em.createQuery("SELECT u from Director u "+
                "WHERE u.nombre = :name" )
                .setParameter("name", par.getNombre());
        try {
            prov = (Director) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            prov = (Director) q.getResultList().get(0);
            
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return prov;
        }
    }
     
     
     //Metodo para traer solo los actores
     public List<Person> AllActors(){
        EntityManager em = emf.createEntityManager();
        List<Person> cat = new ArrayList<Person>();
        Query q= em.createQuery("SELECT u FROM Actor u");
        try {
            List<Person> tempCat = q.getResultList();
        for(Person c: tempCat){
            Person prod = new Person();
            prod.setNombre(c.getNombre());
            prod.setApellido(c.getApellido());
            prod.setFecha(c.getFecha());
            prod.setImagen(c.getImagen());
            prod.setLugarNacimiento(c.getLugarNacimiento());

            cat.add(prod);
        }
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return cat;
        }
    }
     
     
     //Metodo para traer todos los directores
     public List<Person> AllDirectors(){
        EntityManager em = emf.createEntityManager();
        List<Person> cat = new ArrayList<Person>();
        Query q= em.createQuery("SELECT u FROM Director u");
        try {
            List<Person> tempCat = q.getResultList();
        for(Person c: tempCat){
            Person prod = new Person();
            prod.setNombre(c.getNombre());
            prod.setApellido(c.getApellido());
            prod.setFecha(c.getFecha());
            prod.setImagen(c.getImagen());
            prod.setLugarNacimiento(c.getLugarNacimiento());

            cat.add(prod);
        }
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return cat;
        }
    }
     
     //Metodo para traer todos los artistas
     public List<Person> AllArtists(){
        EntityManager em = emf.createEntityManager();
        List<Person> cat = new ArrayList<Person>();
        Query q= em.createQuery("SELECT u FROM Artista u");
        try {
            List<Person> tempCat = q.getResultList();
        for(Person c: tempCat){
            Person prod = new Person();
            prod.setNombre(c.getNombre());
            prod.setApellido(c.getApellido());
            prod.setFecha(c.getFecha());
            prod.setImagen(c.getImagen());
            prod.setLugarNacimiento(c.getLugarNacimiento());

            cat.add(prod);
        }
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return cat;
        }
    }
    
}
